package com.ccs.loas;

import java.util.concurrent.TimeUnit;

import com.ccs.testcase.*;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Miscellaneous {
	
	Loa_NotAllCovered a = new Loa_NotAllCovered();
	Loa_To_Slmc b = new Loa_To_Slmc();
	
	public void go_to_Loa(WebDriver driver){
		//driver.findElement(By.xpath("//li[contains(., 'LOA')]")).click();
		driver.findElement(By.xpath("//div[@id='mainContent']/div[4]/nav/ul/li[2]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void create_button_loa(WebDriver driver){
		driver.findElement(By.xpath("//input[@id='btnCreateLoa']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public String random_number(){
		double num =  Math.floor((Math.random()*9999)+1);
		String code = String.valueOf(num);
		code = code.replaceAll("\\.","");
		return code;
	}
	public void loa_buttons(WebDriver driver, String option, String no){
		String mainWindow = driver.getWindowHandle();
		if(option.equals("draft")){
			driver.findElement(By.xpath("//input[@value='Save As Draft']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			driver.findElement(By.xpath("//button[contains(.,'Yes')]")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}else if(option.equals("final")){
			driver.findElement(By.xpath("//input[@value='Generate LOA']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			driver.findElement(By.xpath("//button[contains(.,'Yes')]")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}else if(option.equals("approve")){
			driver.findElement(By.xpath("//input[@value='Approve']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			String code = random_number();
			String init_app_code = "loa"+code;
			setAppCode(init_app_code);
			driver.findElement(By.xpath("//input[@id='approvalCodeEnter']")).sendKeys(init_app_code);
			driver.switchTo().window(mainWindow);
			driver.findElement(By.xpath("//html/body/div[4]/div[3]/div/button[1]")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			driver.findElement(By.xpath("//html/body/div[2]/div[3]/div/button[1]")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}else if(option.equals("disapprove")){
			driver.findElement(By.xpath("//input[@value='Disapprove']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			driver.findElement(By.xpath("//textarea[@id='disapprovalReason']")).sendKeys("Disapproved LOA");
			driver.findElement(By.xpath("//button[contains(.,'OK')]")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			driver.findElement(By.xpath("//button[contains(.,'Yes')]")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}else if(option.equals("revision")){
			driver.findElement(By.xpath("//input[@value='For Revision']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			driver.findElement(By.xpath("//button[contains(.,'Yes')]")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}else if(option.equals("submit")){
			String appCode = driver.findElement(By.xpath("//input[@id='appCode']")).getAttribute("value");
			if(no.equals("1")){
				System.out.println(appCode);
				System.out.println(Loa_To_Slmc.appCode);
				String init = Loa_To_Slmc.appCode.toUpperCase().toString();
				if(init.equals(appCode)){
					System.out.println("Approval Code is CORRECT");
				}else{
					System.out.println("Approval Code is INCORRECT");
				}
			}else if (no.equals("2")){
				System.out.println(appCode);
				System.out.println(Loa_Admin.appCode);
				String init = Loa_Admin.appCode.toUpperCase().toString();
				if(init.equals(appCode)){
					System.out.println("Approval Code is CORRECT");
				}else{
					System.out.println("Approval Code is INCORRECT");
				}
			}else{
				System.out.println(appCode);
				System.out.println(Loa_NotAllCovered.appCode);
				String init = Loa_NotAllCovered.appCode.toUpperCase().toString();
				if(init.equals(appCode)){
					System.out.println("Approval Code is CORRECT");
				}else{
					System.out.println("Approval Code is INCORRECT");
				}
			}
			driver.findElement(By.xpath("//input[@value='Submit to SLMC']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			driver.findElement(By.xpath("//button[contains(.,'Yes')]")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			driver.findElement(By.xpath("//html/body/div[5]/div[3]/div/button")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}else if(option.equals("save")){
			driver.findElement(By.xpath("//input[@value='Save Changes']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			driver.findElement(By.xpath("//button[contains(.,'Yes')]")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}else{
			System.out.println("Invalid command on LOA page");
		}	
	}
	public void setAppCode(String appCode){
		Loa_NotAllCovered.appCode = appCode;
		Loa_To_Slmc.appCode = appCode;
		Loa_Admin.appCode = appCode;
	}
	public void add_another_remarks(WebDriver driver){
		driver.findElement(By.xpath("//div[@id='containerAdd']/div[1]/div[1]/div/button")).click();
		driver.findElement(By.xpath("//input[@id='txt-1']")).sendKeys("Lorem Ipsum (2)");
		driver.findElement(By.xpath("//div[@id='containerAdd']/div[2]/div/div/button")).click();
		driver.findElement(By.xpath("//div[@id='containerAdd']/div[1]/div[1]/div/button")).click();
		driver.findElement(By.xpath("//input[@id='txt-1']")).sendKeys("Lorem Ipsum (2)");
		System.out.println("Added New Remark on Loa Form");
	}
	public void update_doctors(WebDriver driver){
		
		driver.findElement(By.xpath("//input[@value='Update Doctors']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		String message = driver.findElement(By.xpath("//span[@id='spanAlertMessageLOA']")).getText();
		System.out.println(message);
		
		if(message.equals("No updates on doctor/s.")){
			driver.findElement(By.xpath("//input[@value='Cancel']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}else{
			loa_buttons(driver, "save", "");
		}
		
		
	}	
	public void edit_discharged_loa(WebDriver driver){
		
		String message = driver.findElement(By.xpath("//div[@id='patientDischarged']/div")).getText();
		if(message.equals("Patient is already discharged. LOA can no longer be edited.")){
			System.out.println("Cannot Edit Loa Discharged in HCS: CORRECT");
		}else{
			System.out.println("Cannot Edit Loa Discharged in HCS: INCORRECT");
		}
		
	}
	public void onload_loa_page(WebDriver driver){
		driver.manage().window().maximize();
		go_to_Loa(driver);
		String getLoadValue = driver.findElement(By.xpath("//select[@id='loaStatus']/option[2]")).getAttribute("selected");
		String getLoadText = driver.findElement(By.xpath("//select[@id='loaStatus']/option[2]")).getText();
		System.out.println("The "+getLoadText+ " is "+getLoadValue);
	}
	public void cancel_button_loa_create_search(WebDriver driver){
		go_to_Loa(driver);
		create_button_loa(driver);
		driver.findElement(By.xpath("//button[@id='cancelButton']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//input[@id='loa-number']"));
	}
	public void invalid_dates(WebDriver driver){
		go_to_Loa(driver);
		create_button_loa(driver);
		WebElement startDate = driver.findElement(By.xpath("//input[@id='startdate']"));
		WebElement endDate = driver.findElement(By.xpath("//input[@id='enddate']"));
		
		startDate.sendKeys("2222");
		driver.findElement(By.xpath("//div[@id='addCreateLoaDialog']/div/div/fieldset/button[1]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		WebDriverWait waits = new WebDriverWait(driver,30);
		waits.until(
		        ExpectedConditions.visibilityOfElementLocated(
		             By.xpath("//span[@id='spanAlertCreateLOA']")));
		
		WebElement alert = driver.findElement(By.xpath("//span[@id='spanAlertCreateLOA']"));
		String alert_message1 = alert.getText();
		//System.out.println(alert_message1);
		if(alert_message1.equals("Please enter end date")){
			System.out.println(alert_message1+" : IS CORRECT");
		}else{
			System.out.println(alert_message1+" : IS INCORRECT");
		}
		driver.findElement(By.xpath("//div[@id='addCreateLoaDialog']/div/div/fieldset/button[2]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		endDate.sendKeys("2222");
		driver.findElement(By.xpath("//div[@id='addCreateLoaDialog']/div/div/fieldset/button[1]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		waits.until(
		        ExpectedConditions.visibilityOfElementLocated(
		             By.xpath("//span[@id='spanAlertCreateLOA']")));
		
		String alert_message2 = alert.getText();
		//System.out.println(alert_message2);
		if(alert_message2.equals("Please enter start date")){
			System.out.println(alert_message2+" : IS CORRECT");
		}else{
			System.out.println(alert_message2+" : IS INCORRECT");
		}
			
		startDate.sendKeys("2222");
		driver.findElement(By.xpath("//div[@id='addCreateLoaDialog']/div/div/fieldset/button[1]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		waits.until(
		        ExpectedConditions.visibilityOfElementLocated(
		             By.xpath("//span[@id='spanAlertCreateLOA']")));
		
		String alert_message3 = alert.getText();
		//System.out.println(alert_message3);
		if(alert_message3.equals("Please enter valid format of start date")){
			System.out.println(alert_message3+" : IS CORRECT");
		}else{
			System.out.println(alert_message3+" : IS INCORRECT");
		}
		
		driver.findElement(By.xpath("//button[@id='cancelButton']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void search_functions_loa(WebDriver driver, String option, String value){
		
		driver.findElement(By.xpath("//input[@id='reset']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		WebElement loaNo = driver.findElement(By.xpath("//input[@id='loa-number']"));
		WebElement pinLastName = driver.findElement(By.xpath("//input[@id='pin-lastname']"));
		Select location = new Select(driver.findElement(By.xpath("//select[@id='location']")));
		Select status = new Select(driver.findElement(By.xpath("//select[@id='loaStatus']")));
		
		if(option.equals("loa_number")){
			System.out.println("Testing: Search by Loa Number");
			loaNo.sendKeys(value);
			location.selectByVisibleText("Global City");
			status.selectByVisibleText("ALL");
		}else if(option.equals("pin")){
			System.out.println("Testing: Search by Pin Number");
			pinLastName.sendKeys(value);
			location.selectByVisibleText("Global City");
			status.selectByVisibleText("ALL");
		}else if(option.equals("last_name")){
			System.out.println("Testing: Search by Last Name");
			pinLastName.sendKeys(value);
			location.selectByVisibleText("Global City");
			status.selectByVisibleText("ALL");
		}else if(option.equals("quezoncity")){
			System.out.println("Testing: Search by Location >> Quezon City");
			location.selectByVisibleText("Quezon City");
			status.selectByVisibleText("ALL");
		}else if(option.equals("approved")){
			System.out.println("Testing: Search by Status >> Approved");
			location.selectByVisibleText("Global City");
			status.selectByVisibleText("APPROVED");
		}else if(option.equals("disapproved")){
			System.out.println("Testing: Search by Status >> Disapproved");
			location.selectByVisibleText("Global City");
			status.selectByVisibleText("DISAPPROVED");
		}else if(option.equals("slmc")){
			System.out.println("Testing: Search by Status >> Submitted to SLMC");
			location.selectByVisibleText("Global City");
			status.selectByVisibleText("Submitted to SLMC");
		}else if(option.equals("reissued")){
			System.out.println("Testing: Search by Status >> Reissued");
			location.selectByVisibleText("Global City");
			status.selectByVisibleText("REISSUED");
		}else if(option.equals("new")){
			System.out.println("Testing: Search by Status >> New");
			location.selectByVisibleText("Global City");
			status.selectByVisibleText("NEW");
		}else if(option.equals("pending")){
			System.out.println("Testing: Search by Status >> Pending");
			location.selectByVisibleText("Global City");
			status.selectByVisibleText("PENDING");
		}
			
		
		driver.findElement(By.xpath("//input[@value='Search']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		System.out.println(driver.findElement(By.xpath("//div[@id='data_info']")).getText());
	}
	public void clear_tab_search_loa(WebDriver driver){
		
		go_to_Loa(driver);
		create_button_loa(driver);
		WebElement startDate = driver.findElement(By.xpath("//input[@id='startdate']"));
		WebElement endDate = driver.findElement(By.xpath("//input[@id='enddate']"));
		driver.findElement(By.xpath("//input[@id='createLoaSearchText']")).sendKeys("PIN");
		driver.findElement(By.xpath("//div[@id='addCreateLoaDialog']/div/div/fieldset/button[2]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		startDate.sendKeys("222");
		driver.findElement(By.xpath("//input[@id='startdate']")).sendKeys(Keys.TAB);
		System.out.println("TAB works!");
		endDate.sendKeys("222");

		driver.findElement(By.xpath("//div[@id='addCreateLoaDialog']/div/div/fieldset/button[2]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		driver.findElement(By.xpath("//button[@id='cancelButton']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
	}
	public void end_driver(WebDriver driver){
		driver.quit();
	}
}
